<?php

class Router{
    protected $routes=[];
    
    private function registerRoute($method,$uri,$controller){
        $this->routes[]=[
            'method'=>$method,
            'uri'=>$uri,
            'controller'=>$controller
        ];
    }

/**
 * 添加一个GET路由
 * 
 * @param string $uri
 * @param string $controller
 * @return void
 */
public function addGet($uri,$controller){
    $this->registerRoute('GET',$uri,$controller);
}

/**
 * 添加一个POST路由
 * 
 * @param string $uri
 * @param string $controller
 * @return void
 */
public function addPost($uri,$controller){
    $this->registerRoute('POST',$uri,$controller);
}

/**
 * 添加一个DELETE路由
 * 
 * @param string $uri
 * @param string $controller
 * @return void
 */
public function addDelete($uri,$controller){
    $this->registerRoute('DELETE',$uri,$controller);
}

/**
 * 加载错误页面
 * @param int $httpCode
 * @return void
 */
public function error($httpCode = 404)
{
    http_response_code($httpCode);
    loadView("error/{$httpCode}");
    exit;
}

/**
 * 执行路由
 * @param string $uri
 * @param string $method
 * @return void
 */

public function route($uri,$method){
    foreach($this->routes as $route){
        if($route['uri']===$uri&&$route['method']===$method)
        {
            require basePath($route['controller']);
            return;
        }
    }

    $this->error();
    }

}
